<?php
/**
 * signup.php 注册
 * Jack最初通过该页面注册为系统的用户。
 */
require_once 'setting.php';
require_once 'smarty.php';

require_once ROOTPATH . 'functions.php';
require_once ROOTPATH . 'errorhandler.php';

require_once INCLUDESPATH . 'db/User.php';
require_once INCLUDESPATH . 'db/UserDAO.php';

# start the session function
if (!isset($_SESSION)) {
	session_start();
}

$smarty = new MySmarty();

// if the user already logined, jump to the home page
if (isset($_SESSION['user']) || isRememberMe()) {
	header("Location: index.php");
	exit;
}

// if next action is signup
if (isset($_GET['next']) && $_GET['next'] == "signup") {

	# get the user's input info
	$email = isset($_POST['email']) ? $_POST['email'] : '';
	$username = isset($_POST['username']) ? $_POST['username'] : '';
	$password = isset($_POST['password']) ? $_POST['password'] : '';
	$repassword = isset($_POST['repassword']) ? $_POST['repassword'] : '';
	if (isset($_POST['ownquestion']) && $_POST['ownquestion'] != '') {
		$question = isset($_POST['ownquestion']) ? $_POST['ownquestion'] : '';
	} else {
		$question = isset($_POST['question']) ? $_POST['question'] : '';
	}
	$answer = isset($_POST['answer']) ? $_POST['answer'] : '';
	$verification = isset($_POST['verification']) ?
	strtoupper($_POST['verification']) : '';

	# check if the input is empty
	if ($email == '') {
		registerErrorHandler("Please enter an email.");
		return;
	}
	if ($username == '') {
		registerErrorHandler("Please enter an username.");
		return;
	}
	if ($password == '') {
		registerErrorHandler("Please enter the password.");
		return;
	}
	if ($question == '' || $question == 'choosequestion') {
		registerErrorHandler("Please choose a question.");
		return;
	}
	if ($answer == '') {
		registerErrorHandler("Please enter the answer.");
		return;
	}
	if ($verification == '') {
		registerErrorHandler("Please enter the verifiction.");
		return;
	}

	# check the CAPTCHA
	if (!(validCAPTCHA($verification))) {
		registerErrorHandler("Invalid verification string");
		return;
	} elseif ($_SESSION['captcha'] !== $verification) {
		registerErrorHandler("Wrong verification string");
		return;
	}

	# check the email address
	if (!validEmail($email)) {
		registerErrorHandler("Invalid email address, please choose another one.");
		return;
	} elseif (emailExists($email)) {
		registerErrorHandler("This email is already used, please choose another one.");
		return;
	}

	# check the  username
	if (!validUsername($username)) {
		registerErrorHandler("Invalid username, please choose another one.");
		return;
	} elseif (usernameExists($username)) {
		registerErrorHandler("This username is already used, please choose another one.");
		return;
	}

	# check the password and repassword
	if (!validPassword($password)) {
		registerErrorHandler("Invalid password, please choose another one.");
		return;
	} elseif (!(validPasswords($password, $repassword))) {
		registerErrorHandler("The password and repassword are not equal.");
		return;
	}

	# check the question and answer
	if (!(validQuestion($question) && validAnswer($answer))) {
		registerErrorHandler("Invalide question and answer.");
		return;
	}

	# << begin - Save the user info into the database
	$user = new User();
	$user->setUsername($username);
	$user->setNickname($username);
	$user->setPassword(md5($password));
	$user->setEmail($email);
	$user->setQuestion($question);
	$user->setAnswer(md5($answer));
	
	$userdao = new UserDAO();
	$userdao->insert($user);
	# >> end - Save the user info into the database 
	
	# << begin - Sign up successfully 
	$smarty->assign('title', $pagetitle['signup_c']);
	$smarty->display("signup_complete.tpl");
	# >> end - Sign up successfully 
} else {
	if (isset($_SESSION['errormessage'])) {
		$smarty->assign('errormessage', $_SESSION['errormessage']);
		unset($_SESSION['errormessage']);
	}

	if (isset($_SESSION['email'])) {
		$smarty->assign('email', $_SESSION['email']);
		unset($_SESSION['email']);
	}

	if (isset($_SESSION['regusername'])) {
		$smarty->assign('username', $_SESSION['regusername']);
		unset($_SESSION['regusername']);
	}
	$smarty->assign('title', $pagetitle['signup']);
	$smarty->assign('captcha', 'captcha.php');
	$smarty->display("signup.tpl");
}

/**
 * The register function's error handler
 *
 * @param string $errormessage
 */
function registerErrorHandler($errormessage) {
	global $email, $username;
	$_SESSION['errormessage'] = $errormessage;
	$_SESSION['email'] = $email;
	$_SESSION['regusername'] = $username;
	header("Location: signup.php");
}
?>